use lettre::message::header::ContentType;
use lettre::message::Mailbox;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Address, Message, SmtpTransport, Transport};
fn main() {
    // 发送
    let send_mbox = Mailbox::new(
        Some(String::from("监控提示")),
        Address::new("addstar", "foxmail.com").unwrap(),
    );
    // 回复
    let reply_mbox = Mailbox::new(
        Some(String::from("管理员")),
        Address::new("luojiaxin", "shinva.com").unwrap(),
    );
    //接收
    let resv_mbox = Mailbox::new(
        Some(String::from("用户1")),
        Address::new("luojiaxin", "shinva.com").unwrap(),
    );
    //服务器
    let smtp_username = "addstar@foxmail.com";
    let smtp_server = "smtp.qq.com";
    //授权码
    let smtp_password = "uwdyuhxkxhpqbdfe";
    let email: Message = Message::builder()
        .from(send_mbox) //发
        .reply_to(reply_mbox) //回复人
        .to(resv_mbox) //收
        .subject("Happy new year")
        .header(ContentType::TEXT_PLAIN)
        .body(String::from("Be happy!"))
        .unwrap();

    let creds = Credentials::new(smtp_username.to_owned(), smtp_password.to_owned());

    // Open a remote connection to gmail
    let mailer = SmtpTransport::relay(smtp_server)
        .unwrap()
        .credentials(creds)
        .build();

    // Send the email
    match mailer.send(&email) {
        Ok(_) => println!("Email sent successfully!"),
        Err(e) => panic!("Could not send email: {e:?}"),
    }
}
